package csc.daonjpa.java.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import csc.daonjpa.java.domain.Account;
import csc.daonjpa.java.domain.BaseInfo;
import csc.daonjpa.java.domain.Customer;
import csc.daonjpa.java.domain.TargetAccount;
import csc.daonjpa.java.domain.Transactions;
import csc.daonjpa.java.service.BankService;
import csc.daonjpa.java.service.BaseInfoService;
import csc.daonjpa.java.service.CustomerService;
import csc.daonjpa.java.service.TargetService;
import csc.daonjpa.java.service.TransactionService;

@Controller
public class TransferController {
	private static final Logger LOG = Logger.getLogger(AccountController.class);

	private BankService bankService;
	private CustomerService customerService;
	private TargetService targetService;
	private BaseInfoService baseInfoService;
	private TransactionService transactionService;
	
	public TransactionService getTransactionService() {
		return transactionService;
	}
	@Autowired
	public void setTransactionService(TransactionService transactionService) {
		this.transactionService = transactionService;
	}
	public BaseInfoService getBaseInfoService() {
		return baseInfoService;
	}
	@Autowired
	public void setBaseInfoService(BaseInfoService baseInfoService) {
		this.baseInfoService = baseInfoService;
	}
	public TargetService getTargetService() {
		return targetService;
	}
	@Autowired
	public void setTargetService(TargetService targetService) {
		this.targetService = targetService;
	}

	public CustomerService getCustomerService() {
		return customerService;
	}

	@Autowired
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public BankService getBankService() {
		return bankService;
	}

	@Autowired
	public void setBankService(BankService bankService) {
		this.bankService = bankService;
	}

	/**
	 * 
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/transferMoney", method = RequestMethod.GET)
	public String transferMoney(HttpSession session, Model model) {
		String loginId = session.getAttribute("loginSession").toString();
		List<TargetAccount> lstTarget = targetService.getTargetAccounts();
		Customer customer = customerService.getCustomer(loginId);
		List<Transactions> listTransactions = transactionService.getTransactions();
		model.addAttribute("listTransactions", listTransactions);
		model.addAttribute("lstTargets", lstTarget);
		model.addAttribute("customer",customer);
		return "transferMoney";
	}	
	@RequestMapping(value = "/transferMoney",method = RequestMethod.POST)
	public String transfer(HttpServletRequest request, Model model) {
		Long id_source = Long.parseLong(request.getParameter("sourceAccount"));
		String accNum_target = request.getParameter("targetAccount");
		Double amount  = Double.parseDouble(request.getParameter("amount"));
		Account account = bankService.getAccount(id_source);
		TargetAccount targetAccount = targetService.getTargetAccount(accNum_target);
		BaseInfo baseInfo =  baseInfoService.getBaseInfo(account.getNumber());
		
		if (baseInfo.getAvailableAmount() > amount) {
			Transactions transaction = new Transactions(account,targetAccount, amount, new Date());
			transactionService.addTransaction(transaction);
			Double newMoney = baseInfo.getAvailableAmount() - amount;
			baseInfoService.updateBaseInfo(newMoney, account.getNumber());
			model.addAttribute("successMessage","Transferd money successfully");
		} else {
			model.addAttribute("errorMessage", "Amount > Available Money");
		}
		return "forward:/transferMoney";
	}
//	@ExceptionHandler(Exception.class)
//	public ModelAndView handleAllException(Exception ex) {
//
//		ModelAndView model = new ModelAndView("transferMoney");
//		model.addObject("errorMessage", "Exception occurred");
//
//		return model;
//	}
}
